package com.situ.mall.service.impl;

import com.situ.mall.mapper.CartMapper;
import com.situ.mall.pojo.entity.Cart;
import com.situ.mall.pojo.query.CartQuery;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public void add(Cart cart) {
        Cart c = cartMapper.selectByUserIdAndProductId(cart);
        if (c != null) {
            cart.setId(c.getId());
            cart.setQuantity(cart.getQuantity() + c.getQuantity());
            cartMapper.updateByPrimaryKeySelective(cart);
        } else {
            cartMapper.insertSelective(cart);
        }
    }

    @Override
    public List<CartVO> list(CartQuery cartQuery) {
        return cartMapper.list(cartQuery);
    }

    @Override
    public void update(Cart cart) {
        cartMapper.updateByPrimaryKeySelective(cart);
    }

    @Override
    public void updateByUserId(Cart cart) {
        cartMapper.updateByUserIdSelective(cart);
    }

    @Override
    public void deleteById(Integer id) {
        cartMapper.deleteByPrimaryKey(id);
    }
}